<?php

namespace C\M;

use C\L\Model;
use C\M\Contract;

class GroupBuyingOrder extends Model
{

    public function initialize()
    {
        $this->belongsTo(
            'user_id',
            'C\M\User',
            'uid',
            ['alias' => 'User']
        );
        $this->belongsTo(
            'group_buying_goods_id',
            'C\M\GroupBuyingGoods',
            'id',
            ['alias' => 'GroupBuyingGoods']
        );
    }

    public function beforeValidationOnCreate()
    {
        $this->sn = (new \DateTime())->format('YmdHisu');
        $this->addtime = $this->uptime = time();
        return true;
    }

    public function beforeValidationOnUpdate()
    {
        $this->uptime = time();
        return true;
    }

    public function beforeCreate()
    {
        $oldOtherInfo = json_decode($this->other_info, true);

        $user = $oldOtherInfo['user'] ?? null;
        $userLevel = ['apr' => 0];
        if ($this->user) {
            $user = [
                'id'     => $this->user->uid,
                'mobile' => $this->user->mobile,
            ];
            $userLevel = $this->di['s_level']->getLevel($this->user->uid);
        }

        $goods = [
            'id'        => $this->groupBuyingGoods->id,
            'thumbnail' => $this->groupBuyingGoods->thumbnail,
            'title'     => $this->groupBuyingGoods->title,
            'price'     => $this->groupBuyingGoods->price,
            'content'   => $this->groupBuyingGoods->content,
        ];

        $goodsOtherInfo = json_decode($this->groupBuyingGoods->other_info, true);

        $contractId = $goodsOtherInfo['contract_id'] ?? null;

        $groupBuying = [
            'quantity_min'    => $goodsOtherInfo['group_buying_quantity_min'] ?? null,
            'quantity_max'    => $goodsOtherInfo['group_buying_quantity_max'] ?? null,
            'period'          => $goodsOtherInfo['group_buying_period'] ?? null,
        ];

        $commission = [
            'rate'      => $goodsOtherInfo['commission_rate'] ?? 0,
            'period'    => $goodsOtherInfo['commission_period'] ?? null,
            'type'      => $goodsOtherInfo['commission_type'] ?? null,
            'vip_bonus' => $goodsOtherInfo['commission_vip_bonus'] ?? false,
            'vip_rate'  => $userLevel['apr'] ?? 0,
        ];

        $newOtherInfo = [
            'contract_id'  => $contractId,
            'user'         => $user,
            'goods'        => $goods,
            'group_buying' => $groupBuying,
            'commission'   => $commission,
        ];

        $this->state = $this->quantity >= $groupBuying['quantity_max']
            ? 'success'
            : 'processing';

        $this->other_info = json_encode(array_merge($oldOtherInfo, $newOtherInfo));

        $quantity = (float)$this->quantity ?? 0;
        $price    = (float)$goods['price'] ?? 0;

        $this->amount = $quantity * $price;
    }

    public function getContract()
    {
        $otherInfo = json_decode($this->other_info, true);

        return Contract::findById($otherInfo['contract_id'] ?? null);
    }

    public function getCommissionInfo()
    {
        $otherInfo = json_decode($this->other_info, true);

        return $otherInfo['commission'] ?? [];
    }
}
